package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.constants.GlobalExceptionCode;
import cn.itsource.exception.GlobalException;
import cn.itsource.mapper.DriverMapper;
import cn.itsource.mapper.DriverSummaryMapper;
import cn.itsource.pojo.api.DriverResult;
import cn.itsource.pojo.app.vo.SummaryVo;
import cn.itsource.pojo.domain.Driver;
import cn.itsource.pojo.domain.DriverSummary;
import cn.itsource.service.IDriverSummaryService;
import cn.itsource.utils.BitStatesUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>
 * 司机结算对象 服务实现类
 * </p>
 *
 * @author beishan
 * @since 2024-03-25
 */
@Service
public class DriverSummaryServiceImpl extends ServiceImpl<DriverSummaryMapper, DriverSummary> implements IDriverSummaryService {

    @Autowired
    private DriverMapper driverMapper;

    /**
     * 获取司机的取消订单数和投诉数
     * @param driverId
     */
    @Override
    public DriverResult getDriverSummary(Long driverId) {
        if (driverId == null) {
            throw new GlobalException(GlobalExceptionCode.PARAM_ERROR);
        }
        DriverSummary summary = super.getById(driverId);
        if (summary == null) {
            throw new GlobalException(GlobalExceptionCode.PARAM_ERROR);
        }

        Integer todayCancel = summary.getTodayCancel();
        Integer todayComplaint = summary.getTodayComplaint();
        return  new DriverResult(todayCancel, todayComplaint,StpUtil.getLoginIdAsLong());

    }
    @Override
    public void creatDriver(Long id) {
        DriverSummary driverSummary = new DriverSummary();
        driverSummary.setId(id);
        driverSummary.setLevel(0);
        driverSummary.setTotalOrders(0);
        driverSummary.setWeekOrders(0);
        driverSummary.setGoodComments(0);
        driverSummary.setAppeal(0);
        driverSummary.setTotalComplaint(0);
        driverSummary.setTodayTotalCancel(0);
        driverSummary.setDriveDuration(0);
        driverSummary.setTradeOrders(0);
        driverSummary.setTodayTradeOrders(0);
        driverSummary.setTodayComplaint(0);
        driverSummary.setTodayCancel(0);
        driverSummary.setTodayIncome(new BigDecimal("0"));
        driverSummary.setCreateTime(new Date());
        driverSummary.setDeleted(false);
        driverSummary.setVersion(0);

        super.save(driverSummary);
    }

    /**
     * 工作台渲染
     */
    @Override
    public SummaryVo workBenchData() {
        Driver driver = driverMapper.selectById(StpUtil.getLoginIdAsLong());
        //校验是否完成认证
        if (BitStatesUtil.hasState(driver.getBitState(),BitStatesUtil.OP_REAL_AUTHENTICATIONED)){
            DriverSummary summary = super.getById(StpUtil.getLoginIdAsLong());
            return SummaryVo.builder()
                    .driveDuration(summary.getDriveDuration())
                    .todayIncome(summary.getTodayIncome())
                    .todayTradeOrders(summary.getTodayTradeOrders())
                    .realAuthSuccess(true).build();
        }else {
            throw new GlobalException(GlobalExceptionCode.DRIVER_MATERIAL_EXPIRE);
        }

    }


}
